package com.fdq.demo.tool;

import com.alibaba.fastjson.JSONObject;
import com.fdq.demo.entity.Student;
import com.fdq.demo.entity.StudentResult;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author: 83778
 * @date: 2023-11-14 20:58
 * @desc:
 */
public class SpiderTool {



    public static List<Student> getStudentJson(String token){

        Integer current = 1 ;

        Integer total = 15;


        List<Student> students = new ArrayList<>();

        String json = null;

        for ( ; current <= total ; current++) {

            String url = "https://gateway.jkqingman.com/business/formalStudentDistribution/crm/page?formalPeriodId=60&gradeId=78&nickName=&groupStatus=&hasMobile=&refundStatus=&refundStatusStage=&current="+current+"&size=10";

            try {
                json = Jsoup.connect(url)
                        .timeout(30000)
                        .header("Accept", "*/*")
                        .header("Host", "gateway.jkqingman.com")
                        .header("Accept-Encoding", "gzip, deflate, br")
                        .header("Accept-Language", "zh-CN,zh;q=0.9")
                        .header("X-Requested-With","XMLHttpRequest")
                        .header("Content-Type", "application/javascript; charset=utf-8")
                        .ignoreContentType(true)
                        .header("Authorization",token)
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                        .get()
                        .body()
                        .text();

                //添加json过期逻辑
                if (json.contains("\"code\":401")){
                    break;
                }else {
                    StudentResult studentResult = JSONObject.parseObject(json, StudentResult.class);
                    List<Student> records = studentResult.getStudentData().getRecords();
                    System.out.println("第"+current +"次-->" + records);
                    records.stream().forEach(student -> students.add(student));
                    TimeUnit.SECONDS.sleep(2);
                }

            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }

       }
        return students;

    }

    public static void main(String[] args) {
        List<Student> studentJson = getStudentJson("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJOb25jZSI6ImYzYzVjZTY1LWVmYmMtNDUxNS1iYWEyLTQxZDYwZmZmNDQ2MyIsIlVzZXJJZCI6Ijg2IiwiU3lzVHlwZSI6IjMifQ.7_isJUMbfu6EwRMjRlDCMei9hIo9g0ceVIZykXwFRMU");
        System.out.println("studentJson = " + studentJson);

    }
}
